package cn.gistone.visualeditor.config;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
     
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
     
    /**
     * @author： lw
     * @version: 1.0
     * @date： 2021/1/29
     * @description： 全局配置LocalDateTime，实现格式化
     */
     
    @Configuration
    public class LocalDateTimeSerializerConfig {
     
        // 此处通过Value注解读取配置文件中的 date-format
        @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
        private String pattern;
     
        @Bean
        public LocalDateTimeSerializer localDateTimeDeserializer() {
            return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
        }
     
        @Bean
        public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
            return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
        }
     
    }